|
|
|
|
|
|
|
In the EmployeeDisplay procedure, no changes are made to the arguments strFirst and strLast. However, in other procedures, you may make changes. Consider the following procedure: |
|
|
|
 |
|
|
|
|
Private Sub ProcByRef(ByRef intArg1 As Integer, _
ByRef strArg2 As String)
intArg1 = 20
strArg2 = "Gates"
End Sub |
|
|
|
|
|
|
|
|
The ProcByRef procedure accepts two arguments: an integer called intArg1 and a string called strArg2. Notice that the ProcByRef routine does change the values of the arguments. Lets see what happens when we call this procedure and pass in variables as parameters to this procedure: |
|
|
|
 |
|
|
|
|
Private Sub cmdReference_Click ()
Dim intRet As Integer
Dim strRet As String
intRet = 10
strRet = "Bill"
Call ProcByRef(intRet, strRet)
Me.Print intRet ' Prints 20
Me.Print strRet ' Prints "Gates"
End Sub |
|
|
|
|
|
|
|
|
The event procedure cmdReference_Click() declares two local variables. One is an integer initialized to 10, and the other is a string initialized to the value Bill. These two variables are then passed to the procedure ProcByRef(). After the procedure ProcByRef() has executed, whats the value of the variables? You cant tell just by looking at the routine. If you look at the procedure definition for ProcByRef(), you see that the two parameters are received by reference. This means that the addresses of the parameters passed to the routine are whats actually passed. Therefore, any change made in the ProcByRef() procedure to the two arguments is reflected in the calling procedure. In Figure 9.3 you can see lines from both the calling procedure and the called procedure point to the same memory location in the computer. This is because the parameters are being passed by reference. |
|
|
|
|
|